package com.back.campus_back.service.impl;


import com.back.campus_back.domain.*;
import com.back.campus_back.mapper.GoodsMapper;
import com.back.campus_back.mapper.OrdersMapper;
import com.back.campus_back.mapper.ShoppingCartMapper;
import com.back.campus_back.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-17
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    ShoppingCartMapper shoppingCartMapper;


    //获取所有的订单id
    @Override
    public List getOrderGroup(int user_id){
        List<Orders> orderGroup = ordersMapper.getOrderGroup(user_id);
        System.out.println(orderGroup);
        return orderGroup;
    }

    /*
     * 查询订单信息按照时间降序显示
     * */
    @Override
    public List getOrders(int user_id) {
        // 获取所有的订单id
        Object[] orderGroup = getOrderGroup(user_id).toArray();
        // 获取所有的订单详细信息
        Object[] orders = ordersMapper.getOrders(user_id).toArray();
        //最终返回的订单数组
        List ordersList=new ArrayList();

        // 生成每个订单的详细信息列表
        for (int i = 0; i < orderGroup.length; i++) {
            Orders orderID =(Orders) orderGroup[i];
            List tempOrder=new ArrayList();

            for(int j = 0 ; j < orders.length ; j++) {

                OrdersVo order = (OrdersVo) orders[j];
                //当订单id相等时候说明这商品是在同一个订单里面
                Long orderID2=orderID.getOrderId();
                Long order2=order.getOrderId();
                if (orderID2.equals(order2)){
                    //要加进同一个订单的list里面
                    tempOrder.add(order);
                }
            }
            ordersList.add(tempOrder);
        }
        return ordersList;
    }

    /*
     * 生成订单
     * */
    @Override
    public String addOrders(List<OrdersAddVo> orders) {
        //将传过来的订单转换成Object数组
        Object[] objects = orders.toArray();
        //这是初始化添加订单的结果
        int result =0;
        //将转换的Object数组再次转换成订单对象，因为订单只有一个，所以拿第一个元素
        OrdersAddVo order = (OrdersAddVo) objects[0];
        //在order对象里面有一个Goods列表（因为一个订单可能有多个商品），我们将它取出，并且转为数组对象
        Object[] goods = order.getGoods().toArray();
        order.setCreateTime(new Date());
        System.out.println(goods);
        //遍历goods数组，当有多个商品时候，添加订单的次数=数组的长度
        for (int j = 0; j < goods.length; j++) {
            //将遍历的数组元素转为一个Goods对象
            Goods good = (Goods) goods[j];
            //拿到商品的id
            int goodid = good.getId();
            //设置商品状态为2：1是正常发布状态,2是已经被购买状态,0是下架了商品的状态
            good.setStatus(2);
            //3执行更新操作
            goodsMapper.updateById(good);

            //把订单的商品id赋值为刚才拿到商品的id，在同一个订单中，除了商品id不一样其他都一样，所以只需要拿到商品的id
            order.setGoodsId(goodid);
            //将商品的所属用户id放在订单的卖家id中
            order.setSellerId(good.getStudentId());

            result = ordersMapper.insert(order);
            if (result == 1) {
                shoppingCartMapper.deleteShoppingCart(order.getUserId(),goodid);
            }
        }

        if (result == 1) {
            return "订单已生成";
        } else {
            return "订单生成失败";
        }
    }

    //查询所有订单
    @Override
    public List findAllOrders() {
        List allOrders = ordersMapper.findAllOrders();
        return allOrders;
    }


}
